package com.wyg.blog.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyg.blog.mapper.CommentMapper;
import com.wyg.blog.model.blog.Comment;
import com.wyg.blog.model.blog.MyComment;
import com.wyg.blog.service.blog.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: WYG
 * @time: 2020/7/2 21:24
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;

    @Override
    public IPage<Comment> selectSomeComments(Page<?> page) {
        return commentMapper.getSomeComments(page);
    }

    @Override
    public List<MyComment<Comment>> selectCommentsByBid(Integer bid) {
        List<Comment> comments = commentMapper.getCommentsByBid(bid);
        List<MyComment<Comment>> myComments = new ArrayList<>();
        List<Comment> parentComments = new ArrayList<>();
        int commentsSize = comments.size();
        for(int i = 0; i < commentsSize; i++){
            Comment comment = comments.get(i);
            if(comment.getParent_id() == null){
                parentComments.add(comment);
            }
        }
        for(int i = 0; i < parentComments.size(); i++){
            Comment comment = parentComments.get(i);
            List<Comment> childrenComments = new ArrayList<>();
            for(int j = 0; j < commentsSize; j++){
                Comment children = comments.get(j);
                if(children.getParent_id() == comment.getId()){
                    childrenComments.add(children);
                }
            }
            MyComment<Comment> myComment = new MyComment<Comment>(comment, childrenComments);
            myComments.add(myComment);
        }
        return myComments;
    }

    @Override
    public Comment selectCommentById(Integer id) {
        return commentMapper.getCommentById(id);
    }

    @Override
    public Integer selectCommentCountByBid(Integer id) {
        return commentMapper.getCommentCountByBid(id);
    }

    @Override
    public String selectContentById(Integer id) {
        return commentMapper.getContentById(id);
    }

    @Override
    public boolean insertComment(Comment comment) {
        return commentMapper.addComment(comment);
    }

    @Override
    public boolean renewCommentState(Integer id, boolean state) {
        return commentMapper.updateCommentState(id, state);
    }

    @Override
    public boolean renewComment(Integer id, String content) {
        return commentMapper.updateComment(id, content);
    }

    @Override
    public boolean removeComment(Integer id) {
        return commentMapper.deleteComment(id);
    }

    @Override
    public String selectEmail(Integer id) {
        return commentMapper.getEmailById(id);
    }

    @Override
    public boolean selectState(Integer id) {
        return commentMapper.getStateById(id);
    }
}
